package com.halflife.comeElectricity.basic.datadic;

import com.halflife.framework.converter.MapHandler;
import com.halflife.framework.domain.Page;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class DataDicTypeAdminController {

    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private DataDicTypeRepository dataDicTypeRepository;

    @RequestMapping("/report/list")
    public String gotoReportList(@RequestParam(required = false) HashMap<String,Object> queryParam, Page page, HttpServletResponse response,
                           HttpServletRequest request, Model model) {

        return "report/list";
    }

    @RequestMapping("/fengkong/list")
    public String gotofengkongList(@RequestParam(required = false) HashMap<String,Object> queryParam, Page page, HttpServletResponse response,
                           HttpServletRequest request, Model model) {

        return "fengkong/list";
    }

    @RequestMapping("/fengkong/operate")
    public String gotofengkongOperate(Integer id,HttpServletResponse response,
                              HttpServletRequest request, Model model) {

        return "fengkong/operate";
    }
    @RequestMapping("/chongdian/list")
    public String gotochongdianList(@RequestParam(required = false) HashMap<String,Object> queryParam, Page page, HttpServletResponse response,
                           HttpServletRequest request, Model model) {

        return "chongdian/list";
    }

    @RequestMapping("/chongdian/operate")
    public String gotochongdianOperate(Integer id,HttpServletResponse response,
                              HttpServletRequest request, Model model) {

        return "chongdian/operate";
    }
    @RequestMapping("/huodong/list")
    public String gotoHuodongList(@RequestParam(required = false) HashMap<String,Object> queryParam, Page page, HttpServletResponse response,
                           HttpServletRequest request, Model model) {

        return "huodong/list";
    }

    @RequestMapping("/huodong/operate")
    public String gotohuodongOperate(Integer id,HttpServletResponse response,
                              HttpServletRequest request, Model model) {

        return "huodong/operate";
    }

    @RequestMapping(value = {"/dataDicType/list/{catagory}","/dataDicType/list"})
    public String gotoList(@RequestParam(required = false) HashMap<String,Object> queryParam, Page page, HttpServletResponse response,
                           HttpServletRequest request, Model model,@PathVariable(required = false )String catagory) {
        MapHandler.handleMap(queryParam);
        if (page == null) {
            page = new Page();
        }
        page.setQueryParam(queryParam);
        if (StringUtils.isNotBlank(catagory)) {
            queryParam.put("catagory", catagory);
        }else{
            catagory = "";
        }
        int totalCount = dataDicTypeRepository.selectCount(queryParam);
        List<DataDicType> list = dataDicTypeRepository.selectListByPage(queryParam);
        page.setTotalCount(totalCount);
        page.setMode(model);
        model.addAttribute("catagory",catagory);
        model.addAttribute("list",list);
        model.addAttribute("queryParam",queryParam);
        return "dataDicType/list";
    }
    @RequestMapping("/dataDicType/chargeStrategy/list")
    public String gotoChargeStrategyList(@RequestParam(required = false) HashMap<String,Object> queryParam, Page page, HttpServletResponse response,
                           HttpServletRequest request, Model model) {
        MapHandler.handleMap(queryParam);
        if (page == null) {
            page = new Page();
        }
        page.setQueryParam(queryParam);
        queryParam.put("catagory", "chargeStrategy");
        int totalCount = dataDicTypeRepository.selectCount(queryParam);
        List<DataDicType> list = dataDicTypeRepository.selectListByPage(queryParam);
        page.setTotalCount(totalCount);
        page.setMode(model);
        model.addAttribute("list",list);
        model.addAttribute("queryParam",queryParam);
        model.addAttribute("catagory", queryParam.get("catagory"));
        return "dataDicType/chargeStrategyList";
    }
    @RequestMapping("/dataDicType/discountStrategy/list")
    public String gotoDiscountStrategyList(@RequestParam(required = false) HashMap<String,Object> queryParam, Page page, HttpServletResponse response,
                           HttpServletRequest request, Model model) {
        MapHandler.handleMap(queryParam);
        if (page == null) {
            page = new Page();
        }
        page.setQueryParam(queryParam);
        queryParam.put("catagory", "discountStrategy");
        int totalCount = dataDicTypeRepository.selectCount(queryParam);
        List<DataDicType> list = dataDicTypeRepository.selectListByPage(queryParam);
        page.setTotalCount(totalCount);
        page.setMode(model);
        model.addAttribute("list",list);
        model.addAttribute("queryParam",queryParam);
        model.addAttribute("catagory", queryParam.get("catagory"));
        return "dataDicType/discountStrategyList";
    }
    @RequestMapping("/backup/deposit/list")
    public String gotoDepositList(@RequestParam(required = false) HashMap<String,Object> queryParam, Page page, HttpServletResponse response,
                           HttpServletRequest request, Model model) {
        MapHandler.handleMap(queryParam);
        if (page == null) {
            page = new Page();
        }
        page.setQueryParam(queryParam);
        int totalCount = dataDicTypeRepository.selectCount(queryParam);
        List<DataDicType> list = dataDicTypeRepository.selectListByPage(queryParam);
        page.setTotalCount(totalCount);
        page.setMode(model);
        model.addAttribute("list",list);
        model.addAttribute("queryParam",queryParam);
        return "dataDicType/list";
    }

    @RequestMapping("/dataDicType/operate")
    public String gotoOperate(Integer id,String catagory,HttpServletResponse response,
                               HttpServletRequest request, Model model) {
        DataDicType dataDicType = new DataDicType();
        if(id!=null){
            dataDicType = dataDicTypeRepository.selectByPrimaryKey(id);
        }else{
            dataDicType = new DataDicType();
            if (StringUtils.isNotBlank(catagory)) {
                dataDicType.setCatagory(catagory);
            }
        }
        model.addAttribute("dataDicType",dataDicType);
        return "dataDicType/operate";
    }

    @PostMapping("/dataDicType/save")
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseEntity save(DataDicType dataDicType, HttpServletRequest request,
                              HttpServletResponse response, Model model) {
        try {
            if(dataDicType.getId()!=null){
                dataDicTypeRepository.updateByPrimaryKey(dataDicType);
            }else {
                dataDicType.init();
                Integer a = dataDicTypeRepository.insert(dataDicType);
            }
        } catch (Exception ex) {
            log.error(ex.toString(),ex);
            return new ResponseEntity(1, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(0, HttpStatus.OK);
    }

    @DeleteMapping("/dataDicType/delete")
    public ResponseEntity setRiderState(Integer id, Integer state) {
        try {
            DataDicType dataDicType = new DataDicType();
            dataDicType.setId(id);
            dataDicType.setValid(Boolean.FALSE);
            dataDicTypeRepository.updateByPrimaryKeySelective(dataDicType);
        } catch (Exception ex) {
            log.error(ex.toString(),ex);
            return new ResponseEntity(1, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(0, HttpStatus.OK);
    }
}
